package yxy.game.pm2.command.plainsarena;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import yxy.cherry.battle.module.Position;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Format;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PlainsArenaMessage;
import yxy.game.pm2.module.hero.FormatHandle;
import yxy.game.pm2.module.plainsarena.PlainsArenaHandle;
import yxy.game.pm2.module.plainsarena.PlainsArenaPlayerModule;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

/** 中原擂台 中原擂台布阵界面 */
@MessageType(proto = Command.PlainsArenaFormatUI, classtype = PlainsArenaMessage.PlainsArenaFormatUIRequest.class)
public class CMD_PlainsArenaFormatUI extends Command<PlainsArenaMessage.PlainsArenaFormatUIRequest> {

	@Override
	public void execute(MessageSource messageSource, PlainsArenaMessage.PlainsArenaFormatUIRequest request) {
        Date date = new Date();
        try {
            String playerCode = messageSource.getTag();

            PlainsArenaMessage.PlainsArenaFormatUIResponse.Builder response = PlainsArenaMessage.PlainsArenaFormatUIResponse.newBuilder();

            Result result = new PlainsArenaHandle().formatInfo(playerCode, response);

            send(messageSource, response, result);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Date date1 = new Date();
        System.out.println("CMD_PlainsArenaFormatUI:"+(date1.getTime()-date.getTime()));

    }


    private JSONArray getPower(String playerCode){
        //给3个队伍每个队伍的英雄战力
        FormatHandle formatHandle = new FormatHandle();
        JSONArray returnArrary = new JSONArray();
        PlainsArenaPlayerModule playerModule = PlainsArenaPlayerModule.get(playerCode);
        ArrayList<Format> formatArrayList = playerModule.getFormat();
        for (Format format : formatArrayList) {
            JSONArray formatHeroArrary = new JSONArray();
            Map<Position, Hero> map = formatHandle.formatHeros(playerCode, format);
            for (Map.Entry<Position, Hero> entry : map.entrySet()) {
                JSONObject heroJson = new JSONObject();
                Hero hero = entry.getValue();
                heroJson.put("code",hero.getCode());
                heroJson.put("power",hero.getPower());
                formatHeroArrary.add(heroJson);
            }
            returnArrary.add(formatHeroArrary);
        }
	    return returnArrary;
    }

	private void send(MessageSource messageSource, PlainsArenaMessage.PlainsArenaFormatUIResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}